package com.qxf.service.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.qxf.dao.LoginDao;
import com.qxf.dao.UserInfoDao;
import com.qxf.exception.UserConfigurationException;
import com.qxf.model.ReturnEvent;
import com.qxf.model.beans.UserSession;
import com.qxf.service.BaseAction;
import com.qxf.util.Constants;

public class UserInfoAction extends BaseAction {
	
	private UserInfoDao userInfoDao;
	private LoginDao loginDao;
	
	static Logger log = Logger.getLogger(UserInfoAction.class);

	@Override
	public ReturnEvent process(HttpServletRequest req, HttpServletResponse resp) {
		String op = req.getParameter("op");
		log.info("op = " + op);
		ReturnEvent responseEvent = new ReturnEvent();
		Object data = null;
		userInfoDao = new UserInfoDao();
		try {
			if("getActivityInfo".equalsIgnoreCase(op)) {
				data = getActivityInfo(req, resp);
			} else if("addActivity".equalsIgnoreCase(op)) {
				data = addActivity(req, resp);
			} else if("saveActivity".equalsIgnoreCase(op)) {
				data = saveActivity(req, resp);
			}
		} catch(UserConfigurationException uce) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			responseEvent.setErrorMessage(uce.getMessage());
			log.info("UserConfigurationException: " + uce.getMessage());
		} catch (Exception e) {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
			e.printStackTrace();
		}
		if(data != null) {
			responseEvent.setReturnCode(Constants.RETURN_SUCCESS);
			responseEvent.setResult(data);
		} else {
			responseEvent.setReturnCode(Constants.RETURN_FAILURE);
		}
		return responseEvent;
	}
	
	private UserSession getActivityInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		UserSession userInfo = (UserSession)req.getSession().getAttribute(Constants.SESSION_KEY);
		if(userInfo != null && userInfo.isLogin()) {
			return userInfo;
		}
		return null;
	}
	
	private String addActivity(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String activityName = req.getParameter("activityName");
		String activityUrl = req.getParameter("activityUrl");
		UserSession userInfo = (UserSession)req.getSession().getAttribute(Constants.SESSION_KEY);
		if(userInfoDao.addActivity(activityName, activityUrl,userInfo.getUserId())) {
			userInfo = getUserInfo(userInfo);
			req.getSession().setAttribute(Constants.SESSION_KEY, userInfo);
			return Constants.RETURN_SUCCESS;
		}
		return null;
	}
	
	private String saveActivity(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String activityId = req.getParameter("activityId");
		String activityName = req.getParameter("activityName");
		String activityUrl = req.getParameter("activityUrl");
		UserSession userInfo = (UserSession)req.getSession().getAttribute(Constants.SESSION_KEY);
		if(userInfoDao.saveActivity(activityId, activityName, activityUrl, userInfo.getUserId())) {
			userInfo = getUserInfo(userInfo);
			req.getSession().setAttribute(Constants.SESSION_KEY, userInfo);
			return Constants.RETURN_SUCCESS;
		}
		return null;
	}
	
	private UserSession getUserInfo(UserSession userSesion) throws Exception {
		loginDao = new LoginDao();
		return loginDao.getUserInfo(userSesion);
	}

}
